package com.zjsru.oneDay202205;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/5/3
 * 重新排列日志文件
 * 所有 字母日志 都排在 数字日志 之前。
 * 字母日志 在内容不同时，忽略标识符后，按内容字母顺序排序；在内容相同时，按标识符排序。
 * 数字日志 应该保留原来的相对顺序。
 *
 * 输入：logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
 * 输出：["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
 * 解释：
 * 字母日志的内容都不同，所以顺序为 "art can", "art zero", "own kit dig" 。
 * 数字日志保留原来的相对顺序 "dig1 8 1 5 1", "dig2 3 6" 。
 *
 */
public class reorderLogFiles {
    class Log{
        int idx, type;
        String ori, sign, content;
        Log(String s, int _idx){
            idx = _idx;
            int n = s.length(), i=0;
            while(i < n && s.charAt(i) != ' ') i++;
            sign = s.substring(0,i);
            content = s.substring(i+1);
            ori = s;
            type = Character.isDigit(content.charAt(0)) ? 1 : 0;
        }
    }
    public String[] reorderLogFiles(String[] logs) {
        int n = logs.length;
        List<Log> list = new ArrayList<>();
        for(int i =0; i<n; i++) list.add(new Log(logs[i],i));
        Collections.sort(list, (a,b) ->{
            if(a.type != b.type) return a.type - b.type;
            if(a.type == 1) return a.idx - b.idx;
            return !a.content.equals(b.content) ? a.content.compareTo(b.content) : a.sign.compareTo(b.sign);
        });
        String[] ans = new String[n];
        for (int i = 0; i < n; i++) {
            ans[i] = list.get(i).ori;
        }
        return ans;
    }
    
    public static void main(String[] args) {
        reorderLogFiles reorderLogFiles = new reorderLogFiles();
        String[] logs = new String[]{"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"};
        System.out.println(reorderLogFiles.reorderLogFiles(logs));
    }
}
